﻿/// <summary>
// DBLinq.MySqlProviders - .NET Providers for MySql using the framework DBLinq under MIT license, author Jiri George Moudry
// http://linq.to/db
// and the CodeFx.MySqlProviders under GNU licence, author Laurent Morisseau
// http://www.codeplex.com/CodeFxMySqlProvider
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>
/// 
/// 
using DBLinq.MySqlProviders;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Collections.Specialized;
using MySql.Data.MySqlClient;
using System;

namespace DBLinq.MySqlProviders.TestProject
{
    
    
    /// <summary>
    ///This is a test class for AspNetRolesDTOTest and is intended
    ///to contain all AspNetRolesDTOTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AspNetRolesDTOTest
    {

        private MySqlConnection conn;
        private uint appid = 0;
        private uint userid = 0;
		
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        [TestInitialize()]
        public void MyTestInitialize()
        {
			ConnectionString.Current.CurrentString = TestHelper.connectionstring;
			conn = TestHelper.Open ();
            TestHelper.DeleteMemberTest(conn);
            TestHelper.DeleteApplicationTest(conn);
            appid = TestHelper.InsertApplicationTest(conn);
            userid = TestHelper.InsertMemberTest(conn, appid);
        }
        
        [TestCleanup()]
        public void MyTestCleanup()
        {

            TestHelper.DeleteMemberTest(conn);
            TestHelper.DeleteRoleTest(conn,appid );
            TestHelper.DeleteApplicationTest(conn);
            TestHelper.Close(conn);
        }

        #endregion


        /// <summary>
        ///A test for UsersInRoles_GetRolesForUser
        ///</summary>
        [TestMethod()]
        public void UsersInRoles_GetRolesForUserTest()
        {
            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);
            TestHelper.InsertUserInRole(conn, userid, roleid);

            /// Tests
            
            try
            {
                StringCollection actual = AspNetRolesDTO.UsersInRoles_GetRolesForUser(appid, userid);
                Assert.IsNotNull(actual);
                Assert.AreEqual<string>(actual[0], TestHelper.RoleName);
            }
            finally
            {

                /// Finally
                TestHelper.DeleteUserinRole(conn, userid);
                TestHelper.DeleteRoleTest(conn, roleid);
            }
        }

        /// <summary>
        ///A test for SelectByFk_ApplicationId
        ///</summary>
        [TestMethod()]
        public void SelectByFk_ApplicationIdTest()
        {

            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);
            
            /// Tests
            try
            {
                IEnumerable<AspNetRole> actual = AspNetRolesDTO.SelectByFk_ApplicationId(appid);
                Assert.IsNotNull(actual);
                foreach (AspNetRole role in actual)
                {
                    Assert.AreEqual<string>(role.RoleName, TestHelper.RoleName);
                }

            }
            finally
            {
                TestHelper.DeleteRoleTest(conn, roleid);
            }
        }

        /// <summary>
        ///A test for Insert
        ///</summary>
        [TestMethod()]
        public void InsertTest()
        {
            AspNetRole role = new AspNetRole();
            role.ApplicationId = appid;
            role.RoleName = TestHelper.RoleName;
            role.Description = TestHelper.RoleName;
            
            try
            {
                AspNetRolesDTO.Insert(role);
                MySqlCommand cmd = new MySqlCommand("SELECT rolename FROM AspNet_Roles WHERE applicationid=?appid order by roleid desc limit 0,1", conn);
                cmd.Parameters.Add(new MySqlParameter("?appid", appid));
                object reader = cmd.ExecuteScalar();
                Assert.IsNotNull(reader);
                Assert.AreEqual(TestHelper.RoleName , reader);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                TestHelper.DeleteRoleTest(conn, appid );
            }
	
        }

        /// <summary>
        ///A test for GetRoleId
        ///</summary>
        [TestMethod()]
        public void GetRoleIdTest()
        {
            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);

            try
            {
                /// Tests
                uint actual = AspNetRolesDTO.GetRoleId(TestHelper.RoleName, appid);
                Assert.AreEqual<uint>(actual, roleid);
            }
            finally
            {
                /// Finally
                TestHelper.DeleteRoleTest(conn, roleid);
            }
        }

        /// <summary>
        ///A test for Exist
        ///</summary>
        [TestMethod()]
        public void ExistTest()
        {
            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);

            try
            {
                /// Tests
                bool actual = AspNetRolesDTO.Exist(TestHelper.RoleName);
                Assert.AreEqual<bool>(actual, true);
            }
            finally
            {

                /// Finally
                TestHelper.DeleteRoleTest(conn, roleid);
            }
        }

        /// <summary>
        ///A test for DeleteByFk_ApplicationId_RoleId
        ///</summary>
        [TestMethod()]
        public void DeleteByFk_ApplicationId_RoleIdTest()
        {
            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);

            try
            {
                AspNetRolesDTO.DeleteByFk_ApplicationId_RoleId(appid, roleid);
                bool actual = AspNetRolesDTO.Exist(TestHelper.RoleName);
                Assert.AreEqual<bool>(actual, false);
            }
            finally
            {
                /// Finally
                TestHelper.DeleteRoleTest(conn, roleid);
            
            }
        }
    }
}
